package cn.wsalix.sys.service.impl;

import java.util.List;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.form.UserForm;
import cn.wsalix.sys.entity.VerifyCode;
import cn.wsalix.sys.form.VerifyCodeForm;
import cn.wsalix.sys.repository.VerifyCodeRepository;
import cn.wsalix.sys.service.VerifyCodeService;
import cn.wsalix.utils.Utils;

//@Aspect
@Service("verifyCodeService")
public class VerifyCodeServiceImpl extends
		CommonServiceImpl<VerifyCodeForm, VerifyCodeRepository, VerifyCode>
		implements VerifyCodeService {
	@Autowired
	private VerifyCodeRepository verifyCodeRepository;

	@Override
	public String module() {
		return "/sys/verify_code";
	}

	@Override
	public VerifyCodeRepository getRepository() {
		return verifyCodeRepository;
	}

	/*
	 * @Pointcut(
	 * "execution(* cn.wsalix.observable.service.impl.RegisterServiceImpl.register(..))"
	 * ) public void checkVerifyCode() { };
	 * 
	 * @Before("checkVerifyCode()")
	 */
	public void before(JoinPoint call) {
		logger.info("【注解-前置通知】:方法正常结束了");
		Object[] args = call.getArgs();
		if (args.length == 1) {
			if (args[0] instanceof UserForm) {
				UserForm form = (UserForm) args[0];
				//checkVerifyCode(form);
			}
		}
	}

	@Transactional
	// @Around("checkVerifyCode()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		// start stopwatch
		Object retVal = pjp.proceed();
		if (retVal instanceof Boolean) {
			Boolean val = (Boolean) retVal;
			if (val) {

			}
		}
		// stop stopwatch
		// 这里，这里，这里添加代码，你就把retVal想干嘛就干嘛，然后再返回
		return retVal;
	}

	// @AfterReturning("checkVerifyCode()")
	public void afterReturn(JoinPoint call) {
		logger.info("【注解-后置通知】:方法正常结束了");
	}

	@Override
	public Class<VerifyCode> getEntityClazz() {
		return VerifyCode.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(VerifyCodeForm form) {
		Map<String, SearchFilter> filters = buildFilter();
		if (form.getCode() != null && !form.getCode().equals("")) {
			filters.put("code",
					new SearchFilter("code", Operator.EQ, form.getCode()));
		}
		if (form.getPhone() != null && !form.getPhone().equals("")) {
			filters.put("phone",
					new SearchFilter("phone", Operator.EQ, form.getCode()));
		}
		if (form.getCodeType() != null && !form.getCodeType().equals("")) {
			filters.put("codeType", new SearchFilter("codeType", Operator.EQ,
					form.getCodeType()));
		}
		return filters;
	}
/*
	@Transactional
	private void checkVerifyCode(UserForm userForm) {
		VerifyCodeForm VerifyCodeForm = new VerifyCodeForm();
		// VerifyCodeForm.setOperateType(userForm.getOperateType());
		VerifyCodeForm.setCodeType(userForm.getOperateType());
		VerifyCodeForm.setPhone(userForm.getUsername());
		VerifyCodeForm.setCode(userForm.getVerifyCode());
		List<VerifyCode> resLst = super.findByForm(VerifyCodeForm);
		if (resLst == null || resLst.isEmpty()) {
			userForm.setCreate(false);
		} else if (resLst.size() == 1) {// 业务有效，设置验证码状态
			VerifyCode VerifyCode = resLst.get(0);
			VerifyCode.setStatus(DataEnum.unValid);
			super.edit(VerifyCode);
			userForm.setCreate(false);
		}
		userForm.setCreate(false);
		// TODO 1，测试，2，记录出错信息
	}*/

	/*
	 * @Transactional public SendForm newVerifyCode(UserForm form) {
	 * disableByPhone(form.getUsername()); VerifyCodeForm verifyCodeCode = new
	 * VerifyCodeForm(); verifyCodeCode.setCode(form.getVerifyCode());
	 * verifyCodeCode.setPhone(form.getUsername());
	 * verifyCodeCode.setName(form.getOperateType());
	 * //VerifyCodeCode.setOperateType(form.getOperateType());
	 * verifyCodeCode.setSmsType(form.getOperateType());
	 * verifyCodeCode.setStatus(DataEnum.valid); super.add(verifyCodeCode);
	 * SendForm sendForm = BeanMapper.map(verifyCodeCode, SendForm.class);
	 * return sendForm; }
	 */

	@Transactional
	private void disableByPhone(String phone) {
		verifyCodeRepository.disableByPhone(FlagEnum.unValid, phone);
	}

	@Transactional
	@Override
	public void register(UserForm form) {
		// 开始发送消息
		String verifyCode = "" + Utils.nextInt(1000, 9999);
		form.setVerifyCode(verifyCode);
		/*
		 * SendForm sendForm=newVerifyCode(form); sendForm.setCode(verifyCode);
		 * sendForm.setName("");// 注册是不知用户名
		 * sendForm.setPhone(form.getUsername());
		 * sendForm.setSmsType(SmsEnum.register); senderProxy.send(sendForm);
		 */
		// HttpUtils.smsSingleSend(smsConfig, form.getUsername(), verifyCode);
		logger.info("send sms code(" + verifyCode + ") for ("
				+ form.getUsername() + ")");

	}

	@Transactional
	@Override
	public void restPwd(UserForm form) {
		String verifyCode = "" + Utils.nextInt(1000, 9999);
		form.setVerifyCode(verifyCode);
		/*
		 * SendForm sendForm=newVerifyCode(form); sendForm.setCode(verifyCode);
		 * sendForm.setName("");// 注册是不知用户名
		 * sendForm.setPhone(form.getUsername());
		 * sendForm.setSmsType(SmsEnum.restPwd); senderProxy.send(sendForm);
		 */
		// HttpUtils.smsSingleSend(smsConfig, form.getUsername(), verifyCode);
		logger.info("send sms code(" + verifyCode + ") for ("
				+ form.getUsername() + ")");
	}

	@Transactional
	@Override
	public boolean checkCode(UserForm userForm) {
		VerifyCodeForm verifyCodeForm = new VerifyCodeForm();
		//verifyCodeForm.setCodeType(userForm.getOperateType());
		verifyCodeForm.setPhone(userForm.getUsername());
		verifyCodeForm.setCode(userForm.getVerifyCode());
		List<VerifyCode> resLst = super.findByForm(verifyCodeForm);
		if (resLst == null || resLst.isEmpty()) {
			return false;
		} else if (resLst.size() == 1) {// 业务有效，设置验证码状态为无效
			VerifyCode VerifyCode = resLst.get(0);
			VerifyCode.setStatus(FlagEnum.unValid);
			super.edit(VerifyCode);
			return true;
		}
		return false;// 存在多个符合要求的记录
	}
}
